package is.billsharing.app;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class UserActivity extends Activity

{
	private static final String PREFS_NAME = "user";
	EditText addfnd_email,delfnd_email;
	Button addfnd_submit,delfnd_submit;
	    @Override
	    public void onCreate(Bundle savedInstanceState) 
	    {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.user);
	        
	        Button addfnd,delfnd,addgrp,delgrp;
	        addfnd=(Button)findViewById(R.id.button1);
	        delfnd=(Button)findViewById(R.id.button2);
	        addgrp=(Button)findViewById(R.id.button3);
	        delgrp=(Button)findViewById(R.id.button4);
	        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
            final int uid = settings.getInt("uid",0);
	       addfnd.setOnClickListener(new View.OnClickListener() 
	        {
	            public void onClick(View v) 
	            {
	                // TODO Auto-generated method stub
	            	
	            	 setContentView(R.layout.addfriend);
	            	 addfnd_email=(EditText)findViewById(R.id.fndemail);
	            	 addfnd_submit=(Button)findViewById(R.id.addfnd);
	            	 addfnd_submit.setOnClickListener(new View.OnClickListener() 
	     	        {
	     	            public void onClick(View v) 
	     	            {
	     	                // TODO Auto-generated method stub

	     	            	ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
	    	            	postParameters.add(new BasicNameValuePair("fndemail", addfnd_email.getText().toString()));
	    	            	postParameters.add(new BasicNameValuePair("uid",""+ uid+""));
	    	            	String response1;
	    	             	try {
	    	             		response1 = CustomHttpClient.executeHttpPost("AddFriend", postParameters);
	    	             		String res=response1.toString();
	    	            	    res= res.replaceAll("\\s+","");
	    	            	    if(res.equals("true"))
	    	            	    {
	    	            	    	Intent intent=new Intent(UserActivity.this,TabsampleActivity.class);
	    	            	    	
	    	            	    	startActivity(intent);
	    	            	    }
	    	            	    	else
	    	            	    	{
	    	            	    		addfnd_email.setError("Sorry we didn't found any existing users with email id you provided");
	    	                	       
	    	            	    	}
	    	            	    	
	    	            	     
	    	            	    	
	    	            	} catch (Exception e) {
	    	            		
	    	            		
	    	            	}
	    	            	
	    	            }
	    	        });
	    	         
	    	        
	    


	     	            }
	     	        });
	     	       

	          
	       
	       delfnd.setOnClickListener(new View.OnClickListener() 
	        {
	            public void onClick(View v) 
	            {
	                // TODO Auto-generated method stub

	            	 setContentView(R.layout.delfriend);
	            	 delfnd_email=(EditText)findViewById(R.id.delfndemail);
	            	 delfnd_submit=(Button)findViewById(R.id.delfnd);
	            	 delfnd_submit.setOnClickListener(new View.OnClickListener() 
	     	        {
	     	            public void onClick(View v) 
	     	            {
	     	                // TODO Auto-generated method stub

	     	            	ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
	    	            	postParameters.add(new BasicNameValuePair("fndemail", delfnd_email.getText().toString()));
	    	            	postParameters.add(new BasicNameValuePair("uid",""+ uid+""));
	    	            	String response1;
	    	             	try {
	    	             		response1 = CustomHttpClient.executeHttpPost("DeleteFriend", postParameters); 
	    	             		//response1 = CustomHttpClient.executeHttpPost("http://192.168.1.108/BillsharingServer/DeleteFriend", postParameters); 
	    	             		
	    	             		String res=response1.toString();
	    	            	    res= res.replaceAll("\\s+","");
	    	            	    if(res.equals("true1"))
	    	            	    {
	    	            	    	Intent intent=new Intent(UserActivity.this,TabsampleActivity.class);
	    	            	    	
	    	            	    	startActivity(intent);
	    	            	    }
	    	            	    	else if(res.equals("false1"))
	    	            	    	{
	    	            	    		TextView err = (TextView) findViewById(R.id.error);
	    	            	    		
	    	                	    }
	    	            	    	
	    	            	     
	    	            	    	
	    	            	} catch (Exception e) {
	    	            		
	    	            		
	    	            	}
	    	            	
	    	            }
	    	        });
	    	           

	            }
	        });
	       
	       addgrp.setOnClickListener(new View.OnClickListener() 
	        {
	            public void onClick(View v) 
	            {
	                
	           	Intent intent=new Intent(UserActivity.this,GroupActivity.class);
        	    	
      	    	startActivity(intent);
	            	   

	            }
	        });
	    }

}

